# _*_ coding: UTF-8 _*_
# 2020/5/5 2:16 
# PyCharm  
# Create by:LIUMINXUAN
# E-mail:liuminxuan1024@gmail.com
import cv2 as cv
import numpy as np


def LinearWindowFilter(src_img, winSize, padding_way='0'):
    # 首先判断winSize是否是奇数，不是奇数则报错退出
    if winSize % 2 == 0 or winSize == 1:
        print('winSize Error! such as:3, 5, 7, 9....')
        return None
    height, width = src_img.shape
    print("行数(高):%d,列数(宽):%d" % (height, width))
    line = list()
    rows = 0
    cols = 0
    while rows < height:
        while cols < width:
            line[0:3] = src_img[rows, cols:cols + winSize]
            line_sort = sorted(line)
            # print(line)
            # print(line_sort)
            list_len = len(line)
            src_img[rows, cols] = line_sort[list_len // 2]
            cols += winSize
        cols = 0
        rows += 1
    cv.imshow("after", src_img)
    cv.waitKey(0)
    cv.destroyAllWindows()


src_jiaoyan = cv.imread("./img/jiaoyan/danghui_jiaoyan.bmp", cv.IMREAD_GRAYSCALE)
src_img = np.copy(src_jiaoyan)
cv.imshow("src_jiaoyan", src_img)
cv.waitKey(0)
cv.destroyAllWindows()
LinearWindowFilter(src_img, 3)
